package com.wandian.demo.controller
{
	import com.wandian.demo.events.GameEvent;
	import com.wandian.demo.ui.LinesContainer;

	import flash.display.DisplayObjectContainer;
	import flash.events.EventDispatcher;

	import utils.UIHelper;

	/**
	 *
	 *
	 * 老虎机选线控制器
	 *  @author long
	 */
	public class LinesController extends EventDispatcher
	{
		// static instance 
		protected static var instance:LinesController=null;
		public static const onSelectLinesChanged:String='onSelectLinesChanged';
		public var allLinesArray:Array=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30];
		public var allLinesX:Array=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
		public var allLinesY:Array=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
		public var selectLines:Array=[0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 28, 29, 30];
		public var linesContainer:LinesContainer;
		public var linesSceenWidth:Number=726;
		public var linesSceenHeight:Number=363;
		public var startX:Number=40;
		public var startY:Number=109;
		public var startCenterY:Number=295;
		public var currentSelectIndex:int=0;

		public function LinesController()
		{
			super();
			if (instance != null)
				throw new Error("Only one Singleton instance should be instantiated");
			instance=this;

		}

		public function getCurrentLinesCount():int
		{
			return this.selectLines[this.currentSelectIndex];
		}

		public function doSelectLines():void
		{
			GameBetController.Instance.currentBet=0;
			if (GameBetController.Instance.spinEnableFun != null)
			{
				GameBetController.Instance.spinEnableFun("btn_spin", false);
			}

			this.currentSelectIndex++;
			if (this.currentSelectIndex < 0 || this.currentSelectIndex > selectLines.length - 1)
			{
				this.currentSelectIndex=0
				hiddenAllLines();
				trace("隐藏画线");
			}
			else
			{
				for (var i:int=1; i <= selectLines[currentSelectIndex]; i++)
				{
					setLinesVisibleByIndex(i - 1, true);
				}
//					trace("画线，count：" + currentSelectIndex + " from " + (int(selectLines[currentSelectIndex - 1]) + 1) + " to " + selectLines[currentSelectIndex]);

			}
			GameBetController.Instance.dispatchEvent(new GameEvent(LinesController.onSelectLinesChanged));
		}

		public function hiddenAllLines():void
		{

			for (var i:int=0; i < linesContainer.numChildren; i++)
			{
				linesContainer.getChildAt(i).visible=false;
			}
		}

		static public function get Instance():LinesController
		{
			if (instance == null)
				instance=new LinesController();
			return instance;
		}

		public function showLinesContainer(parentContainer:DisplayObjectContainer):void
		{
			if (!parentContainer)
				return;
			linesContainer=new LinesContainer();
			parentContainer.addChild(this.linesContainer);
		}

		/**
		 * 根据索引来控制压线是否显示
		 * @param index
		 * @param boolVisible
		 */
		public function setLinesVisibleByIndex(index:int, boolVisible:Boolean):void
		{
			if (index < 0 || index > linesContainer.numChildren - 1)
				return;
			linesContainer.getChildAt(index).visible=boolVisible;
		}
	}
}
